/*Container：容器
InputID：存值ID
ID：分类参数
Type:菜单类型
DefaultStr：默认字符串
Fu:回调函数
CategoryName:分类名
Start:地区开始级别
Stop:地区结束级别*/
function CreateSelect(Config, Data) {
    var C={Container:null,InputID:null,Fu:null,DefaultStr:'',Type:0};
    var D = { ID: 0, dir: '', CategoryName: '', Start: 0, Stop: 0, CategoryID: 0, P: 2 };
    jQuery.extend(D, Data);
    jQuery.extend(C, Config);
    if (typeof C.InputID == "string") C.InputID = $('#' + C.InputID);
    if (typeof C.Container == "string") C.Container = $('#' + C.Container);
    if (C.Container.length == 0 || C.InputID.length == 0) return;
    C.InputID.val(D.ID);
    if (D.dir != "") D.dir += '/';
    $.ajax({
        type: "get",
        url: webpath + "Sys/Ajax/" + D.dir + D.CategoryName + ".ashx", cache: false,
        data: D,
        success: function (Str) {
            C.Container.append(Str);
            if ($('select:last>option', C.Container).length == 0) $('select:last', C.Container).remove();
            if (C.DefaultStr != '') {
                if (C.Type == 0) {
                    $('select', C.Container).prepend('<option value="0">' + C.DefaultStr + '</option>');
                } else {
                    $('select', C.Container).eq(0).prepend('<option value="0">' + C.DefaultStr + '</option>');
                }
            }
            if ($('select:last', C.Container).val() != D.ID) { $('select:last', C.Container).val(['0']) }
            $('select', C.Container).change(function () { ChangeSelect(this); });
            if ($.isFunction(C.Fu)) C.Fu();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
        }
    });
    function ChangeSelect(obj) {
        if (C.Type == 3) {
            C.InputID.val($(obj).val());
            if ($.isFunction(C.Fu)) C.Fu(obj);
            return;
        }
        $(obj).nextAll('select').remove();
        if ($(obj).val() == '0') {
            C.InputID.val($(obj).prev('select').val() || 0);
            if ($.isFunction(C.Fu)) C.Fu(obj);
            return;
        }
        C.InputID.val($(obj).val());
        var d = jQuery.extend({}, D, { P: C.Type, ID: $(obj).val() });
        $.ajax({
            type: "get",
            url: webpath + "Sys/Ajax/" + D.dir + D.CategoryName + ".ashx", cache: false,
            data: d,
            success: function (Str) {
                $(obj).after(Str);
                if (C.Type == 0) {
                    $(obj).nextAll('select').prepend('<option value="0">' + C.DefaultStr + '</option>');
                    if ($('select', C.Container).slice(-1).val() != $(obj).val()) { $('select', C.Container).slice(-1).val(['0']) }
                } else {
                    C.InputID.val($('select', C.Container).slice(-1).val());
                }
                $(obj).nextAll('select').change(function () { ChangeSelect(this); });
                if ($.isFunction(C.Fu)) C.Fu(obj);
            }, dataType: 'text'
        });
    }
}